
#include <js_native_api.h>
#include <js_native_api_types.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

#include "NapiTest.h"
#include "log.h"

#define    TEST_TAG    "Test NAPI"

#define __USE_OBJ__ 1

typedef struct {
    char msg[128];
    int number;
    int (*callback)(void *data);
} Test;

int checkData(void *data) {
    char *msg = (char *)data;
    if (msg == nullptr) {
        return -1;
    }
    HILOGD(TEST_TAG":%s\r\n", msg);
    return 0;
}

Test *CreateTestInfo()
{
    Test *t = (Test *)malloc(sizeof(Test));
    if (t == nullptr) {
        return nullptr;
    }
    memset(t, 0x00, sizeof(Test));
    strcpy(t->msg, "test msg 123!");
    t->number = strlen(t->msg);
    t->callback = checkData;
    HILOGI(TEST_TAG);
    return t;
}

napi_status SetObjectProperty(napi_env env, napi_value &object, const char *name, long type) {
    napi_value value = nullptr;
    napi_value key;
    napi_create_string_utf8(env,name,strlen(name),&key);
    napi_create_int64(env, type, &value);
    return napi_set_property(env, object, key,  value);
}

static napi_value InitTest(napi_env env, napi_callback_info info)
{
    HILOGI(TEST_TAG"%{public}d \r\n", 1111);

    napi_status status;
    napi_value result;
    HILOGI(TEST_TAG);
    Test *t = CreateTestInfo();
    if (t == nullptr) {
        return nullptr;
    }
#if (__USE_OBJ__==0)
    uint64_t addr = reinterpret_cast<int64_t>(t);
    if (napi_create_int64(env, addr, &result) != napi_ok) {
        return nullptr;
    }
    return result;
#else

    status = napi_create_object(env, &result);
    if (status != napi_ok) {
        return nullptr;
    }

    // 将指向“Test”结构的指针存储在JS对象内。
    status = napi_wrap(env, result, t, NULL, NULL, NULL);
    if (status != napi_ok) {
        return nullptr;
    }

    return result;
#endif
}

static napi_value UnInitTest(napi_env env, napi_callback_info info)
{
    HILOGI(TEST_TAG"%{public}d \r\n", 1111);
#if __USE_OBJ__
    napi_status status;
    size_t argc = 1;
    napi_value argv;
    bool is_test_handle;
    
    status = napi_get_cb_info(env, info, &argc, &argv , nullptr, nullptr);
    if (status != napi_ok) {
        return nullptr;
    }

    Test *t;
    status = napi_unwrap(env, argv, (void **)&t);
    if (status != napi_ok || t == nullptr) {
        return nullptr;
    }
    
    if (t->callback) {
        t->callback(t->msg);
    }

    napi_value value;
    napi_create_int32(env, 0, &value);

    return value;
#else
    napi_value result = nullptr;
    size_t argc = 1;
    napi_value args = nullptr;
    
    if (napi_get_cb_info(env, info, &argc, &args, nullptr, nullptr) != napi_ok) {
        return nullptr;
    }
    
    int64_t addr;
    if (napi_get_value_int64(env, args, &addr) != napi_ok) {
        return nullptr;
    }
    Test *t = (Test *)addr;
    if (t->callback) {
        t->callback(t->msg);
    }
    
    napi_value value;
    napi_create_int32(env, 0, &value);
    
    return value;
#endif
}

static napi_value Add(napi_env env, napi_callback_info info)
{
    size_t requireArgc = 2;
    size_t argc = 2;
    napi_value args[2] = {nullptr};
    
    napi_get_cb_info(env, info, &argc, args , nullptr, nullptr);

    napi_valuetype valuetype0;
    napi_typeof(env, args[0], &valuetype0);

    napi_valuetype valuetype1;
    napi_typeof(env, args[1], &valuetype1);

    double value0;
    napi_get_value_double(env, args[0], &value0);

    double value1;
    napi_get_value_double(env, args[1], &value1);

    napi_value sum;
    napi_create_double(env, value0 + value1, &sum);

    return sum;
}

EXTERN_C_START
static napi_value Init(napi_env env, napi_value exports)
{
    HILOGI(TEST_TAG"%{public}d \r\n", 1111);
    
    napi_property_descriptor desc[] = {
        { "add", nullptr, Add, nullptr, nullptr, nullptr, napi_default, nullptr },
        { "TestOpen", nullptr, InitTest, nullptr, nullptr, nullptr, napi_default, nullptr },
        { "TestClose", nullptr, UnInitTest, nullptr, nullptr, nullptr, napi_default, nullptr },
        { "create", nullptr, NapiTest::Create, nullptr, nullptr, nullptr, napi_default, nullptr }
    };

    napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
    
    return NapiTest::Init(env, exports);
}
EXTERN_C_END

static napi_module demoModule = {
    .nm_version =1,
    .nm_flags = 0,
    .nm_filename = nullptr,
    .nm_register_func = Init,
    .nm_modname = "hello",
    .nm_priv = ((void*)0),
    .reserved = { 0 },
};

extern "C" __attribute__((constructor)) void RegisterHelloModule(void)
{
    napi_module_register(&demoModule);
}
